<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="dict in dict.type.pcj_article_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" prop="id" sortable="custom" align="center" min-width="2%" type="index"
                       :index="indexMethod"/>
      <el-table-column label="文章类型" align="center" prop="plateName" width="100"/>
      <el-table-column label="类型分区" align="center" prop="partitionName" width="100"/>
      <el-table-column label="文章标题" align="center" prop="title"/>
      <el-table-column label="文章作者" align="center" prop="userName" width="200"/>
      <el-table-column label="文章状态" align="center" prop="status">
        <!-- 用平台方法回填字典类型值 -->
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pcj_article_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="被收藏量" align="center" prop="collectNum" width="100"/>
      <el-table-column label="被点赞量" align="center" prop="likeNum" width="100"/>
      <el-table-column label="被评论量" align="center" prop="commentNum" width="100"/>
      <el-table-column label="发布时间" align="center" prop="publishTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <!-- fixed = "right" 固定操作列 -->
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pcj:article:edit']"
          >审批
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pcj:article:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import {listArticle, getArticle, delArticle, addArticle, updateArticle} from "@/api/pcj/article";

  export default {
    name: "Article",
    //获取字典-平台字典用法
    dicts: ["pcj_article_status"],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 贴吧-文章表格数据
        articleList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          createTime: null,
          publishTime: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    //进入页面的初始化方法在这写，此时dom元素已初始化结束，在created方法之后执行
    mounted() {
    },
    methods: {
      /** 查询贴吧-文章列表 */
      getList() {
        this.loading = true;
        listArticle(this.queryParams).then(response => {
          this.articleList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 表格翻页序号连续
      indexMethod(index) {
        return (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          delFlag: null,
          createId: null,
          createBy: null,
          createTime: null,
          updateId: null,
          updateBy: null,
          updateTime: null,
          userId: null,
          publishTime: null,
          status: null,
          partitionId: null,
          address: null,
          title: null,
          content: null,
          uploadImgUrl: null,
          collectNum: null,
          likeNum: null,
          commentNum: null,
          remark: null,
          rejectReason: null,
          pulledReason: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加贴吧-文章";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids;
        //打开编辑页面，path路径在/src/route/index.js里面配置,query为参数主要传入id和操作类型opt
        this.$router.push({
          path: "/pcj/pcjArticleInfo",
          query: {
            opt: "edit",
            id: id
          }
        });
      },
      /** 提交按钮 */
     /* submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateArticle(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addArticle(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },*/
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除贴吧-文章编号为"' + ids + '"的数据项？').then(function () {
          return delArticle(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('pcj/article/export', {
          ...this.queryParams
        }, `article_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

